package com.lb.jwitter.action.util;


import java.io.File;
 
import com.lb.jwitter.util.ExcelToJSon;
import com.opensymphony.xwork2.ActionSupport;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.io.FileUtils;



public class FileUploadAction extends ActionSupport{
 

    private File file;

    private String fileContentType;

    private String fileFileName;

    private String msg;

    private String baseFolder;
    
    private String fileId;
    

	private String destFile;
	public String getDestFile() {
		return destFile;
	}

	public void setDestFile(String destFile) {
		this.destFile = destFile;
	}
	
	private int dotDKId;

	public int getDotDKId() {
		return dotDKId;
	}

	public void setDotDKId(int dotDKId) {
		this.dotDKId = dotDKId;
	}

	@Override
    public String execute() throws Exception
    {
        String randomName = "a" + Math.round(Math.random()*100000000000000.0);
        
        fileFileName = fileFileName.replace(" ", "_");

        destFile = "./file/UploadFile/" + randomName + "_" + fileFileName;
               

        fileFileName = randomName + "_" + fileFileName;
        
//        System.out.println(fileFileName);
        
        fileId = fileFileName.split("\\.")[0];
        

        if ( file != null )
        {
            if (  ((file.length() / 1024) / 1024) < 10 )

            {
                FileUtils.copyFile( file, new File( destFile ) );
                msg = "Upload successfully";
            }
            else
            {
                msg = "Size must be less then 10MB";
            }

        }

       // jsons = ExcelToJSon.execute(destFile);
//        System.out.println("Size = " + jsons.size());
//        for(int i = 0; i < jsons.size() ; i++){
//    		JSONObject obj = jsons.getJSONObject(i);
//    	    String tencu = obj.getString("TenCu");  	
//    	    String tenmoi = obj.getString("TenMoi");  
//    	     
//    	    System.out.println( "tencu: " + tencu );
//    	    System.out.println( "tenmoi: " + tenmoi );
//    	}
        return "forward";
    }

    public File getFile()
    {
        return file;
    }

    public void setFile( File file )
    {
        this.file = file;
    }

    public String getFileContentType()
    {
        return fileContentType;
    }

    public void setFileContentType( String fileContentType )
    {
        this.fileContentType = fileContentType;
    }

    public String getFileFileName()
    {
        return fileFileName;
    }

    public void setFileFileName( String fileFileName )
    {
        this.fileFileName = fileFileName;
    }

    public String getMsg()
    {
        return msg;
    }

    public void setMsg( String msg )
    {
        this.msg = msg;
    }


	public String getBaseFolder()
    {
        return baseFolder;
    }

    public void setBaseFolder( String baseFolder )
    {
        this.baseFolder = baseFolder;
    }

	public String getFileId() {
		return fileId;
	}

	public void setFileId(String fileId) {
		this.fileId = fileId;
	}
    
    

}
